﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiArte.Data.Catalog
{
    public class ProductDal : BaseDal
    {
        /// <summary>
        /// Inserts a new product
        /// </summary>
        /// <param name="code">The unique product code</param>
        /// <param name="isNew">True if product is new (appears on homepage)</param>
        /// <param name="form">The form name used in product detail</param>
        /// <returns>The inserted product id</returns>
        public int Insert(String code, Boolean isNew, String form)
        {
            using (var command = new Npgsql.NpgsqlCommand("INSERT INTO product(product_id, code, is_new, form) VALUES " +
                "(nextval('product_id_sequence'), :code, :is_new, :form) RETURNING product_id"))
            {
                command.Parameters.AddWithValue("code", code);
                command.Parameters.AddWithValue("is_new", isNew);
                command.Parameters.AddWithValue("form", form);

                return PerformInsertCommandAndGetReturningValue(command);
            }
        }

        /// <summary>
        /// Deletes all products
        /// </summary>
        public void DeleteAll()
        {
            using (var command = new Npgsql.NpgsqlCommand("DELETE FROM product"))
                PerformCommand(command);
        }
    }
}
